from PyQt5.QtCore import *
from examples.example_calculator.calc_conf import *
#from calc_conf import *#--
from examples.example_calculator.calc_node_base import *
#from calc_node_base import *#--
from nodeeditor.utils import dumpException
import examples.example_calculator.tuoZhan as tuoZhan

class CalcGraphicsNode(QDMGraphicsNode):#节点外壳
    def initSizes(self):
        super().initSizes()
        self.width = 160
        self.height = 160
        self.edge_roundness = 6
        self.edge_padding = 0
        self.title_horizontal_padding = 8
        self.title_vertical_padding = 10

    def initAssets(self):
        super().initAssets()
        self.icons = QImage("icons/status_icons.png")

    def paint(self, painter, QStyleOptionGraphicsItem, widget=None):
        super().paint(painter, QStyleOptionGraphicsItem, widget)

        offset = 24.0
        if self.node.isDirty(): offset = 0.0
        if self.node.isInvalid(): offset = 48.0

        painter.drawImage(
            QRectF(-10, -10, 24.0, 24.0),
            self.icons,
            QRectF(offset, 0, 24.0, 24.0)
        )

class CalcShuChuContent(QDMNodeContentWidget):
    def initUI(self):
        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(10,0,10,10)
        self.setLayout(self.layout)
        
        self.edit = QLineEdit("????", self)
        self.edit.setAlignment(Qt.AlignLeft)
        self.edit.setObjectName(self.node.content_label_objname)
        
        self.edit1 = QLineEdit("这是一个空白的任务模板", self)
        self.edit1.setAlignment(Qt.AlignLeft)
        self.edit1.setObjectName(self.node.content_label_objname)
        
        self.button = QPushButton(text="生成",parent=self)
        
        self.layout.addWidget(self.edit)
        self.layout.addWidget(self.edit1)
        self.layout.addWidget(self.button)
    
    def serialize(self):
        res = super().serialize()
        res['value'] = self.edit.text()
        res['value1'] = self.edit1.text()
        return res

    def deserialize(self, data, hashmap={}):
        res = super().deserialize(data, hashmap)
        try:
            value = data['value']
            self.edit.setText(value)
            value1 = data['value1']
            self.edit1.setText(value1)
            return True & res
        except Exception as e:
            dumpException(e)
        return res


@register_node(OP_NODE_SHUCHU)
class CalcNode_ShuChu(CalcNode):
    icon = "icons/in.png"
    op_code = OP_NODE_SHUCHU
    op_title = "输出"
    content_label_objname = "calc_node_shuchu"
    GraphicsNode_class = CalcGraphicsNode

    def __init__(self, scene):
        super().__init__(scene, inputs=[1], outputs=[])
        self.eval()

    def initInnerClasses(self):
        self.content = CalcShuChuContent(self)
        self.grNode = CalcGraphicsNode(self)
        self.content.edit.textChanged.connect(self.onInputChanged)
        self.content.button.clicked.connect(self.createPyText)

    def evalImplementation(self):
        u_value = self.content.edit.text()
        s_value = int(u_value)
        self.value = s_value
        self.markDirty(False)
        self.markInvalid(False)

        self.markDescendantsInvalid(False)
        self.markDescendantsDirty()

        self.grNode.setToolTip("")

        self.evalChildren()
        print("输出---------------")
        return self.value
    
    def createPyText(self):
        name = self.content.edit.text()
        tuoZhan.createPath(str(name))
        f = open("./Task/" + name + "/job.py","wb")
        
        str01 = self.getInput(0).eval()
        str01 = '    ' + str01.replace('\n','\n    ')#给每一行加上4个空格
        
        s_value = getPyText(self.content.edit1.text(),str01)
        
        f.write(s_value.encode(encoding='utf-8'))
        f.close()

def getPyText(info,info2):
    Py_Data = 'import sys'
    Py_Data = Py_Data + '\n'
    Py_Data = Py_Data + 'sys.path.append("../")'
    Py_Data = Py_Data + '\n'
    Py_Data = Py_Data + 'import time'
    Py_Data = Py_Data + '\n'
    Py_Data = Py_Data + 'from SeerStateLogic import *'
    Py_Data = Py_Data + '\n'
    Py_Data = Py_Data + '\n'
    Py_Data = Py_Data + 'def job():'
    Py_Data = Py_Data + '\n'
    Py_Data = Py_Data + '    #' + str(info)
    Py_Data = Py_Data + '\n'
    Py_Data = Py_Data + str(info2)
    Py_Data = Py_Data + '\n'
    Py_Data = Py_Data + '    pass'
    return Py_Data
